<?php

class Itemdescription_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function get_offer($offer_id)
	{
		$sql = "SELECT * FROM offer AS o 
				JOIN category AS c ON o.category_id = c.category_id
				JOIN sub_category AS s ON o.sub_category_id = s.sub_category_id
				WHERE offer_id = ? ";
		$query = $this->db->query($sql, $offer_id);
		if($query->num_rows() > 0){
			$data = $query->row_array();	
		}
		return $data;
	}
	
	function get_requesters($offer_id)
	{
		$sql = "SELECT * FROM request WHERE offer_id = ? ";
		$query = $this->db->query($sql, $offer_id);
		$data = array();
		if($query->num_rows() > 0)
		{
			foreach($query->result_array() as $row)
				$data[] = $row;
		}
		return $data;
	}
	
	function is_requester($offer_id, $fb_id)
	{
		$sql = "SELECT * FROM request WHERE offer_id = ? AND fb_id= ? ";
		$query = $this->db->query($sql, array($offer_id, $fb_id));
		if($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}
	
	function get_similar_items($category_id, $sub_category_id, $fb_id, $offer_id)
	{
		$sql = "SELECT * FROM offer WHERE category_id = ? AND sub_category_id = ? AND offer_id <> ?";
		$query = $this->db->query($sql,array($category_id, $sub_category_id, $offer_id));
		$data = array();
		if ($query->num_rows() > 0)
		{
			foreach($query->result_array() as $row)
				$data[] = $row;
		}
		return $data;
	}
	
	function get_comments($offer_id, $fb_id)
	{
		$q1 = $this->db->query("SELECT * FROM offer WHERE offer_id = ? ",$offer_id);
		$status = $q1->result_array();
		if ($q1->num_rows() > 0 && ($status[0]['fb_id'] == $fb_id)) // owner
		{
			$q = $this->db->query("SELECT * FROM offer_chat WHERE offer_id = ? ORDER BY date_created ASC", $offer_id);
		}
		else
		{
			$q1 = $this->db->query("SELECT * FROM request WHERE offer_id = ? AND fb_id = ? ", array($offer_id, $fb_id));
			$status1 = $q1->result_array();
			if($q1->num_rows() > 0 && ($status1[0]['status'] == 'Offered to you' || $status1[0]['status'] == 'Closed'))
			{
				$q = $this->db->query("SELECT * FROM offer_chat WHERE offer_id = ? ORDER BY date_created ASC", $offer_id);
			}
			else
			{
				$q = $this->db->query("SELECT * FROM offer_chat WHERE offer_id = ? AND is_private=0 ORDER BY date_created ASC", $offer_id);
			}
		}
		$data = array();
		if ($q->num_rows() > 0)
		{
			foreach($q->result_array() as $row)
				$data[] = $row;
		}
		
		for ($i=0;$i<count($data);$i++)
		{
			$data[$i]['date_created'] = date('d-m-Y H:i:s', strtotime($data[$i]['date_created']));
		}
		return $data;
	}
	
	/*
	function get_new_comments($offer_id, $last)
	{
		$sql = "SELECT * FROM offer_chat WHERE offer_id = ".$offer_id ." AND date_created >= '".$last."' AND date_created <= NOW() ORDER BY date_created ASC";
		$result = $this->db->query($sql);
		
		$new_comments = array();
		if ($result->num_rows() > 0)
		{
			foreach($result->result_array() as $row)
			{
				$new_comments[] = $row;
			}
		}
		return $sql;
	}*/
	
	function insert_comment($comment, $fb_id, $offer_id, $lasttimestamp)
	{
		$q = $this->db->query("SELECT * FROM offer WHERE offer_id = ? ", $offer_id);
		$status = $q->result_array();
		if ($status[0]['status'] == 'Reserved')
			$private = 1;
		else
			$private = 0;
		
		$data = array(
			'offer_id' => $offer_id,
			'poster_fb_id' => $fb_id,
			'is_private' => $private,
		);
		
		$this->db->set('message', strip_tags($comment));
		$this->db->set('date_created', 'NOW()',false);
		$this->db->set($data);
		$this->db->insert('offer_chat', $data);
		
		if ($lasttimestamp != '')
		{
			$lasttimestamp = date('Y-m-d H:i:s', strtotime($lasttimestamp));
			$sql = "SELECT * FROM offer_chat WHERE offer_id = ? AND date_created > ? AND date_created <= NOW() 
					ORDER BY date_created ASC";
			$result = $this->db->query($sql, array($offer_id, $lasttimestamp));
		}
		else {
			$sql = "SELECT * FROM offer_chat WHERE offer_id = ? AND date_created <= NOW() ORDER BY date_created ASC";
			$result = $this->db->query($sql, $offer_id);
		}
		$new_comments = array();
		if ($result->num_rows() > 0)
		{
			foreach($result->result_array() as $row)
			{
				$new_comments[] = $row;
			}
		}
		for ($i=0;$i<count($new_comments);$i++)
		{
			$new_comments[$i]['date_created'] = date('d-m-Y H:i:s', strtotime($new_comments[$i]['date_created']));
		}
		return $new_comments;
	}
	
	function delete_comment($id)
	{
		$this->db->delete('offer_chat', array('chat_id' => $id));
	}
	
	function give_offer($offer_id, $fb_id)
	{
		$sql = "UPDATE request SET status = 'Offered to you' WHERE fb_id = ? AND offer_id = ? AND status='Pending'";
		$this->db->query($sql, array($fb_id, $offer_id));
		$sql = "UPDATE request SET status = 'Offered to someone else' WHERE offer_id = ? AND fb_id <> ?";
		$this->db->query($sql, array($offer_id, $fb_id));
		$sql = "UPDATE offer SET status = 'Reserved' WHERE offer_id = ?";
		$this->db->query($sql, $offer_id);
	}
	
	function accept_offer($offer_id, $fb_id)
	{
		$sql = "UPDATE request SET status = 'Closed', display = 0 WHERE status = 'Offered to you' AND offer_id = ? AND fb_id= ?";
		$this->db->query($sql, array($offer_id, $fb_id));
		$this->db->delete('request', array('offer_id' => $offer_id, 'status' => 'Offered to someone else'));
		$sql = "UPDATE offer SET status = 'Closed', display = 0 WHERE offer_id = ?";
		$this->db->query($sql, $offer_id);
	}
	
	function request_offer($offer_id, $fb_id, $status)
	{
		if ($status == 'reserved')
		{
			$data = array(
				'offer_id' => $offer_id,
				'fb_id' => $fb_id,
				'status' => 'Offered to someone else',
				'date_created' => date('Y-m-d H:i:s')
			);
		}
		else
		{
			$data = array(
				'offer_id' => $offer_id,
				'fb_id' => $fb_id,
				'date_created' => date('Y-m-d H:i:s')
			);
		}
		
		$this->db->insert('request', $data);
		
	}
	
}

?>